$(function() {
	var doc = $(document),
		body = $('body'),
		sideContent = body.find('.side-content'),
		previewContainer = body.find('.preview-container'),
		win = $(window), 
		preview = previewContainer.find('.preview');
	
	$.widget("MQM.sideBar", {

		_create: function () {
			var self = this,
				e = self.element;
				
			e.html(template("side-bar-tmpl"));
			self.top = e.find('.top');
			self.middle = e.find('.middle');
			self.bottom = e.find('.bottom');
			
			self.middle.css('height', sideContent.height() - self.top.height() - self.bottom.height() - 22 + 'px');
			win.resize( function() {
				self.middle.css('height', sideContent.height() - self.top.height() - self.bottom.height() - 22 + 'px');
				e.find('.particles').css('height', self.middle.height() - e.find('.other-input').height() - 1 + 'px');
			});

			self.select = e.find('.type-select').on('change', function (e) {
				self.questionSelect(e);
			});
			
			self.add = e.find('.addUpdate').on('click', function () {
				self.questionAdd();
			});
			
			
		
		},
		
		questionSelect: function (e) {
			var self = this,
				e = self.element;
			
			var qtype = self.select.find(":selected").data('value');
			if (qtype === 'mc-bank') {
				var seed =  MCBank();
				self.setSeed(seed, true);
			} else if (qtype === 'mc') {
				
			}
			
			
			
		},
		
		setSeed: function (seed, newSeed) {
			var self = this;
			
			self.currentSeed = seed;
			self.newSeed = newSeed;
			self.currentSeed.setForm();
		},
		
		questionAdd: function () {
			var seed = MCBank(),
				self = this,
				e = self.element,
				obj = {};
				
			e.find('.seed-data-array').each(function (index, element) {
				element = $(element);
				var key = element.data('attribute'),
					data = element.val();
					
				if (typeof obj[key] === "undefined") {
					obj[key] = [];
				}
				
				if (data !== "") {
					obj[key].push(data);
				}
			});
			
			e.find('.seed-data-single').each(function (index, element) {
				element = $(element);
				var key = element.data('attribute'),
					data = element.val();
				
				if (data !== "") {
					obj[key] = data;
				}
			});
			
			seed.set(obj);
			preview.preview('addSeed', seed);
		}
		
	});
	
	var sidebar = sideContent.sideBar().resizable({
		handles:"e", 
		minWidth:"220",
		maxWidth:"800",
		helper:"ui-resizable-helper",
		stop: function(event, ui) {
			sideContent.css({
				width: sidebar.width() + 'px',
				height: '100%'
			});
			previewContainer.css('width', body.width() - sidebar.width()  - 1 + 'px');
		}
	});


});
var id = 0;
var getUniqueId = function () {
	return id++;
};
